﻿
const E = window.wangEditor

var data = utils.init({
    apiRoute: '/DutyInfo/',
    siteId: utils.getQueryInt('siteId'),

    tableData: [],//表格数据
    page: {
        currentPage: 1, // 当前页码
        pageSize: 20, // 每页的数据条数
        total: 0, // 总条数
    },
    
    columns: [
    //{ prop: 'dutyLeader', label: '带班领导' }
        //, { prop: 'dutyCadre', label: '值班干部' }
    ],
    rules: {
        dutyDate: [
            { required: true, message: '请选择值班时间', trigger: 'blur' },
        ]
    },


    /* 弹出层 */
    formTitle: '新增',
    formVisible: false,
    model: {},//表单对象

    /* 字段编辑 */
    fieldVisble: false,
    fieldTable: [],
    fieldPage: {
        currentPage: 1, // 当前页码
        pageSize: 20, // 每页的数据条数
        total: 0, // 总条数
    },

    
});



var vue = new Vue({
    el: '#main',
    data: data,
    methods: {
        apiGetPage: function () {
            let $this = this,
                page = $this.page;
            utils.loading($this, true);
            $api.get($this.apiRoute + 'getPageList', {
                params: {
                    siteId: $this.siteId,
                    page: page.currentPage,
                    perPage: page.pageSize
                },
            }).then(function (res) {
                $this.tableData = res.data.data;
                vue.$set($this.page, 'total', res.data.total)

            }).catch(function (error) {
                utils.error(error);
            }).then(function () {
                utils.loading($this, false);
            });

        },
        apiGetFieldAll() {
            let $this = this;
            $api.get('/DutyField/getList', {
                params: {
                    siteId: $this.siteId,
                },
            }).then(function (res) {
                let d = res.data.data;
                let arr = [];
                d.map((item) => {
                    let obj = {};
                    obj.prop = item.fieldKey;
                    obj.label = item.fieldName;
                    arr.push(obj)
                });
                $this.columns = arr;
            }).catch(function (error) {
                utils.error(error);
            })
        },


        formClose() {
            this.formTitle = '';
            this.formVisible = false;
        },



        addModel() {
            this.model = {};
            this.formTitle = '新增';
            this.formVisible = true;
        },
        editModel(row) {
            this.model = Object.assign({},row);
            this.dialogformTitleTitle = '编辑：';
            this.formVisible = true;
        },
        //每页条数改变时触发 选择一页显示多少行
        handleSizeChange(val) {
            console.log(`每页 ${val} 条`);
            this.page = { currentPage: 1, pageSize: val };
            this.apiGetPage();
        },
        //当前页改变时触发 跳转其他页
        handleCurrentChange(val) {
            console.log(`当前页: ${val}`);
            this.page.currentPage = val;
            this.apiGetPage();
        },




        onSubmit() {//提交
            let $this = this;
            $this.$refs.form.validate(function (valid, dataOther) {
                if (valid) {
                    $this.model.siteId = $this.siteId;
                    $api.post($this.apiRoute, $this.model)
                        .then((res) => {
                            $this.$message({
                                message: res.data.msg,
                                type: 'success'
                            });
                            $this.apiGetPage();
                            $this.formClose();
                        }).catch((error) => {
                            utils.error(error);
                        })

                } else {
                    let keys = Object.keys(dataOther);
                    $this.$message({
                        message: dataOther[keys[0]][0].message,
                        type: "warning",
                    });
                }
            });
        },
        delModel(row) {
            let $this = this;
            $this.$confirm('此操作将删除【' + row.dutyDate.substr(0, 10) + '】, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                $api.post($this.apiRoute + 'delete', null, { params: { siteId: $this.siteId, id: row.id } })
                    .then(function (res) {
                        $this.apiGetPage();
                        $this.$message({
                            type: 'success',
                            message: res.data.msg
                        });
                    }).catch(function (error) {
                        utils.error(error);
                    })
            }).catch((err) => {
                $this.$message({
                    type: 'info',
                    message: '删除异常' + err
                });
            });
        },






        /* 添加字段start */
        addField() {
            this.apiGetFieldPage().then(res => {
                this.fieldVisble = true;
            });
        },
        fieldClose() {
            this.fieldVisble = false;
            this.apiGetFieldAll();//刷新表格显示字段
        },

        addRow() {
            let rank = { $isEdit: true, fieldName: "", fieldKey: "", sort: undefined };         
            this.fieldTable.push(rank);
        },
        apiGetFieldPage: function () {
            let $this = this,
                page = $this.fieldPage;
            return $api.get('/DutyField/getPageList', {
                params: {
                    siteId: $this.siteId,
                    page: page.currentPage,
                    perPage: page.pageSize
                },
            }).then(function (res) {
                $this.fieldTable = res.data.data;
                vue.$set($this.fieldPage, 'total', res.data.total)
            }).catch(function (error) {
                utils.error(error);
            });
        },
        //每页条数改变时触发 选择一页显示多少行
        fieldSizeChange(val) {
            this.fieldPage = { currentPage: 1, pageSize: val };
            this.apiGetFieldPage();
        },
        //当前页改变时触发 跳转其他页
        fieldCurrentChange(val) {
            this.fieldPage.currentPage = val;
            this.apiGetFieldPage();
        },

        saveField(row) {
            let $this = this;
            row.siteId = $this.siteId;
            row.$isEdit = false;

            $api.post('DutyField', row)
                .then((res) => {
                    $this.$message({
                        message: res.data.msg,
                        type: 'success'
                    });
                }).catch((error) => {
                    row.$isEdit = true;
                    utils.error(error);
                })
        },
        editField(row) {
            row.$isEdit = true;
        },
        delField(scope) {
            let row = scope.row;
            let $this = this;
            if (row.id) {
                $this.$confirm('此操作将删除【' + row.fieldName + '】, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    $api.post('DutyField/delete', null, { params: { siteId: $this.siteId, id: row.id } })
                        .then(function (res) {
                            $this.$message({
                                type: 'success',
                                message: res.data.msg
                            });
                        }).catch(function (error) {
                            utils.error(error);
                        })
                }).catch((err) => {
                    $this.$message({
                        type: 'info',
                        message: '删除异常' + err
                    });
                });
            } else {
                $this.fieldTable.splice(scope.$index, 1);
            }
        },



        /* 添加字段end */
    },
    created: function () {
        this.apiGetPage();
        this.apiGetFieldAll();
    },
});